<?php


namespace app\admin\controller\ord;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="订单列表")
 */
class Order extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\Order();
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as $k => $v){
                //service_gid
                $service_goods = Db::name("service_goods")->where("id",$v["service_gid"])->find();
                if(empty($service_goods["title"])){
                    $v['service_name'] = "";
                }else{
                    $v['service_name'] = $service_goods['title'];
                }
                if(empty($service_goods["zhu_img"])){
                    $v['service_img'] = "";
                }else{
                    $v['service_img'] = $service_goods['zhu_img'];
                }
                $list[$k] = $v;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    //技师列表
    public function qwe(){
        $coupon = Db::name('service_technician')->order('id desc')->field('id,service_name as name,phone as text')->select()->toArray();
        return json($coupon);
    }

    /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['xuanzhong'])){
                $this->error('技师未选中');
            }
            $xuanzhong = explode(',',$post['xuanzhong']);
            $xuanzhong = array_filter($xuanzhong);
            Db::name('order_scanning_code')->where("oid",$post['xid'])->update(["is_cancel"=>1]);
            foreach($xuanzhong as $k=>$v){
                $data = [
                    'service_user_id'=>$v,
                    'oid'=>$post['xid'],
                    "create_time"=>time()
                ];
                Db::name('order_scanning_code')->insert($data);
            }
            $result = [
                'code'=>1,
                'msg' =>'分配成功'
            ];
            return $result;
        }else{
            $post = $this->request->param('id');
            $order_status = Db::name('order')->where("id",$post)->value("status");
            if($order_status == 1 || $order_status == 5 || $order_status == 8){
                return "此订单不能分配技师";
            }
            $order_scanning_code = Db::name('order_scanning_code')->where("oid",$post)->find();
     
            if(!empty($order_scanning_code)){
                if($order_scanning_code["status"] != 0){
                    return "此订单技师已服务，不能更换技师";
               }
            }
            $this->assign('xid',$post);
        }
        return $this->fetch();
    }
    
    /**
    * @NodeAnotation(title="订单详情")
    */
    public function ord_detail()
    {
        $post = $this->request->param();
        $row = Db::name("order")->where("id",$post["id"])->find();
        //商品
        $service_goods = Db::name("service_goods")->where("id",$row["service_gid"])->field("id,title,zhu_img")->find();

        //规格
        $service_duration = Db::name('service_duration')->where('id',$row['spec_id'])->find();
        if(empty($service_duration)){
            //规格为空的时候，在订单详情查
            $order_item = Db::name("order_item")->where("oid",$row["id"])->find();
            $sku_attribute_list =  json_decode($order_item["specs_list"],true);
            $service_goods["price"] = $order_item["price"];
            foreach($sku_attribute_list as $k=>$v){
                $qwelisy = "规格名称：".$k."   规格属性：".$v;
            }
        }else{
            //规格存在的时候直接查
            $sku_attribute_list = json_decode($service_duration["specs_list"],true);
            $service_goods["price"] = $service_duration["price"];
            foreach($sku_attribute_list as $k=>$v){
                $qwelisy = "规格名称：".$k."   规格属性：".$v;
            }
        }
        //地址
        $adderss = Db::name("address")->where("id",$row["address_id"])->find();
        //下单人
        $user = Db::name("user")->where("id",$row["uid"])->field("nickname")->find();

        $row["service_goods"] = $service_goods; 
        $row["adderss"] = $adderss; 
        $row["qwelisy"] = $qwelisy;
        $row["nickname"] = $user["nickname"]; 
        $row["create_time"] = date("Y-m-d H:i:s",$row["create_time"]);

        //查询是否有差价订单 
        $flag = Db::name("order_price_difference")->where("oid",$row["id"])->find();
        if(empty($flag)){
            $row['is_check'] = false;
        }else{
            $row['is_check'] = true;
            $flag["create_time"] = date("Y-m-d H:i:s",$flag["create_time"]);
        }
        $row["chaorder"] = $flag;
        //dump($row);die;
        $this->assign("row",$row);
        return $this->fetch();
    }

    //查看分配技师
    public function stock_url(){
        $post = $this->request->param();
        $order_scanning_code = Db::name("order_scanning_code")->where("oid",$post["id"])->where("is_cancel",0)
                            ->field("id,oid,service_user_id")
                            ->paginate(20)->each(function($item){
                                $service_technician = Db::name("service_technician")->where("id",$item["service_user_id"])->field("id,service_name")->find();
                                $item["service_name"] = $service_technician["service_name"];
                                return $item;
                            });
         $order_scanning_code = $order_scanning_code->toArray();
         $this->assign('data',$order_scanning_code["data"]);
        return $this->fetch();
    }

    /**
    * @NodeAnotation(title="修改订单为待服务状态")
    */
    public function modify_state()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $ret = $this->model->where('id', $post['id'])->where('status', 6)->find();
            if (empty($ret)) {
                $this->error('当前订单状态不能修改');
            }
            $this->model->where('id', $post['id'])->update(["status"=>2,"is_order_refund"=>0]);
            $this->success('修改成功', '');
        }
        return $this->fetch();
    }

    
 
} 